#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import jieba
import pymysql
from wordcloud import WordCloud

commodity_id = 100005332318

font = r'C:\Windows\Fonts\FZSTK.TTF'
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root',
                       database='jd_comment')
cursor = conn.cursor()
sql = """
    SELECT content FROM `comment` WHERE commodity_id=%s
"""
cursor.execute(sql, commodity_id)
result = cursor.fetchall()
comment_list = []
for row in result:
    comment_list.append(row[0].split())
cursor.close()
conn.close()


stop_word_list = ['手机', '感觉', '真的', '京东', '红魔', '努比亚', '评论', '评价', '体验', '朋友']
for line in open('stop_word_file\百度停用词表.txt', encoding="utf-8"):
    stop_word_list.append(line.strip())

comment = jieba.cut(str(comment_list), cut_all=False)
words = ' '.join(comment)
wc = WordCloud(width=1080, height=2340, background_color='white', font_path=font,
               stopwords=stop_word_list, contour_width=3, contour_color='steelblue')
wc.generate(words)
wc.to_file('红魔3.png')
